package userloginterceptor

import (
	"context"
	"encoding/json"
	"github.com/go-playground/validator"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/model/adminmodel"
	"vpn-management-server/src/model/userlogmodel"
	"vpn-management-server/src/service/adminservice"
	"vpn-management-server/src/service/userservice"
)

func GetPageLogValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var getLogs userlogmodel.GetPageLogs
	err := json.Unmarshal(request.Parameters, &getLogs)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to getUserlog error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(getLogs)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// 审计管理员 超级管理员 用户组管理员
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.AuditAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&getLogs)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetAllLogValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var getLogs userlogmodel.GetAllLogs
	err := json.Unmarshal(request.Parameters, &getLogs)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to getUserlog error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(getLogs)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// 审计管理员 超级管理员 用户组管理员
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.AuditAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&getLogs)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetLogByUserIDValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var getLogsByUserID userlogmodel.GetLogsByUserID
	err := json.Unmarshal(request.Parameters, &getLogsByUserID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to getUserlog error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(getLogsByUserID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// 审计管理员 超级管理员 用户组管理员
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.AuditAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&getLogsByUserID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetPageLogsByUGroupIDValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var getLogsByUGroupID userlogmodel.GetPageLogsByUGroupID
	err := json.Unmarshal(request.Parameters, &getLogsByUGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetPageLogsByUGroupID error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(getLogsByUGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.AuditAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&getLogsByUGroupID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetAllLogsByUGroupIDValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var getLogsByUGroupID userlogmodel.GetAllLogsByUGroupID
	err := json.Unmarshal(request.Parameters, &getLogsByUGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetPageLogsByUGroupID error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(getLogsByUGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.AuditAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&getLogsByUGroupID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func UpLoadNormalLogValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var userLog userlogmodel.UserLog
	err := json.Unmarshal(request.Parameters, &userLog)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to getUserlog error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(userLog)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	user := userservice.GetUserByName(userLog.Name)
	if user == nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user by name fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "get user by name fail"
		return false
	}
	userLog.UGroupID = user.UGroupID
	request.SetModel(&userLog)
	return true
}

func UpLoadWarningLogValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var warningLog userlogmodel.UserLog
	err := json.Unmarshal(request.Parameters, &warningLog)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to getUserlog error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(warningLog)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	user := userservice.GetUserByName(warningLog.Name)
	if user == nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user by name fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "get user by name fail"
		return false
	}
	warningLog.UGroupID = user.UGroupID
	request.SetModel(&warningLog)
	return true
}
